/**
 * 多线程同时操作一个资源（买火车票的例子）
 * 多个线程操作同一个资源的情况下，线程不安全
 */
public class TestThread03 implements Runnable {

    private int ticketNums = 10;

    @Override
    public void run() {
        while (true){
            if (ticketNums<=0){
                break;
            }
            //模拟延迟
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"拿到了第"+ ticketNums-- +"张票");
        }
    }
    public static void main(String[] args) {
        TestThread03 testThread03 = new TestThread03();

        new Thread(testThread03,"张三").start();
        new Thread(testThread03,"李四").start();
        new Thread(testThread03,"王五").start();
    }
}
